package com.gs.m_person_details.model

import android.content.Context
import android.util.Log
import com.gs.common.API
import com.gs.common.callback.HttpCallback
import com.gs.common.callback.impl.CommonHttpCallback
import com.gs.common.constant.HttpCode
import com.gs.common.constant.StateCode
import com.gs.m_person_details.constant.UpdateErrorStateCode
import okhttp3.Call
import okhttp3.Response
import org.json.JSONObject
import java.net.ConnectException
import java.net.SocketTimeoutException

class ChangeInfoModel(context: Context) {

    private val api by lazy { API.getInstance() }

    fun updateUserInfo(
        username: String, changeNickname: String? = null,
        changeUsername: String? = null, changePhoneNumber: String? = null,
        block: (Int) -> Unit
    ) {
        block.invoke(StateCode.STATE_LOADING)
        api.updateUserInfo(
            username,
            changeNickname,
            changeUsername,
            changePhoneNumber,
            object : HttpCallback {
                override fun onSuccess(call: Call, result: String, response: Response) {
                    val cookies = response.header("Set-Cookie")
                    Log.d("cookies", "$cookies")
                    val resultJSON = JSONObject(result)
                    Log.d("result", result)
                    Log.d("code", "${resultJSON["code"]}")
                    if (resultJSON["code"] == HttpCode.SUCCESS_CODE) {
                        block.invoke(StateCode.STATE_SUCCESS)
                        return
                    }
                    when (resultJSON["msg"]) {
                        "用户名已存在" -> {
                            block.invoke(UpdateErrorStateCode.STATE_USERNAME_EXISTED)
                        }

                        "手机号已存在" -> {
                            block.invoke(UpdateErrorStateCode.STATE_PHONE_NUMBER_EXISTED)
                        }

                        else -> block.invoke(StateCode.STATE_FAILED)
                    }
                }

                override fun onError(call: Call, e: Exception) {
                    when (e) {
                        is ConnectException -> {
                            block.invoke(StateCode.NETWORK_ERROR)
                        }

                        is SocketTimeoutException -> {
                            block.invoke(StateCode.SERVER_ERROR)
                        }

                        else -> {
                            block.invoke(StateCode.UNKNOWN_ERROR)
                        }
                    }
                }

            })
    }

    companion object {
        private var instance: ChangeInfoModel? = null

        fun getInstance(context: Context): ChangeInfoModel {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = ChangeInfoModel(context)
                    }
                }
            }
            return instance!!
        }
    }
}